﻿//#define USINGACCESSDB
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using YNCNumberSys.ZJClass;
using Microsoft.VisualBasic.Devices;
using System.Data.SqlClient;
using myDBDataAdapter = System.Data.SqlClient.SqlDataAdapter;
using myDBConnection = System.Data.SqlClient.SqlConnection;
using myDBTransaction = System.Data.SqlClient.SqlTransaction;
using cadApp = Autodesk.AutoCAD.ApplicationServices.Application;
using myDBCommand = System.Data.SqlClient.SqlCommand;
using YNCNumberSys.db;
using YNCNumberSys.DbDal;
using common;

namespace YNCNumberSys
{
    /// <summary>
    /// 4C无图申请。
    /// </summary>
    public partial class Apply4CNumberNoDwg : Form
    {
        private string s_用户名;
        public string str_用户名
        {
            get { return s_用户名; }
            set
            {
                s_用户名 = value;
                this.tbx_用户名.Text = value;
            }
        }

        public Apply4CNumberNoDwg()
        {
            InitializeComponent();
            tbx_设计日期.Text = System.DateTime.Now.ToString("yyyy-MM-dd");
            string filePath = System.Reflection.Assembly.GetExecutingAssembly().Location;
            string file = Path.Combine(Path.GetDirectoryName(filePath), "config.ini");
            if (File.Exists(file))
            {
                FileIni t = new FileIni(file);
                string regKey = t.ReadValue("配置", "4C无图机床");
                if (!string.IsNullOrEmpty(regKey))
                {
                    tbx_机床型号.Text = regKey;
                }

                string regKey2 = t.ReadValue("配置", "4C无图A号");
                if (!string.IsNullOrEmpty(regKey2))
                {
                    tbx_A号.Text = regKey2;
                }
            }
        }
        /// <summary>
        /// 系统要号，内部调用存储过程：
        /// （1）Lock USER
        /// （2）建立文件的临时文件夹，创建对应的Table TempFolder
        /// （3）CheckFileNo
        /// （4）调用存储过程，内部逻辑：分配图号，已经分配过的，不进行分配。未分配过的，进行分配。
        /// （5）文件重新命名。FilesCopyNewAndDelete
        /// （6）重新设置标题栏。SetTitle。
        /// （7）unLock USER
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btn_执行_Click(object sender, EventArgs e)
        {
            this.labelOut.Text = "消息";
            this.labelOut.Update();
            if (tbx_A号.Text.Trim() == string.Empty 
                || tbx_机床型号.Text.Trim() == string.Empty)
            {
                cadApp.ShowAlertDialog("机床型号、A号不能为空！");
                return;
            }
            StatusDal statusDal = new StatusDal();
            string isLock = statusDal.IsLock(s_用户名);
            if (isLock != "ok")
            {
                cadApp.ShowAlertDialog(isLock);
                return;
            }
            else
            {
                DataTable dtLock = statusDal.Lock(s_用户名);
                if (dtLock.Rows.Count == 1)
                {
                    if ((string)dtLock.Rows[0]["user"] == s_用户名)
                    {
                        ProductInfoDal productinfodal = new ProductInfoDal();
                        productinfodal.UpdateProductInfoDatable(
                            tbx_机床型号.Text.Trim(),
                            tbx_A号.Text.Trim());
                        TempFolderDal tempfolderdal = new TempFolderDal();
                        DataTable dtFolder = tempfolderdal.Build4CNoDwgTable();
                        TempFolderDal folderDal = new TempFolderDal();
                        folderDal.InsertTempFolder(dtFolder);
                        Apply4CNumberDal ApDal = new Apply4CNumberDal();
                        DateTime t1 = DateTime.Now;
                        DataTable dtCheck = ApDal.CheckFileNo(tbx_机床型号.Text.Trim(), tbx_A号.Text.Trim(), s_用户名);
                        if (dtCheck.Rows.Count == 0)
                        {
                            t1 = DateTime.Now;
                            DataTable dt;
                            if (this.checkBoxIncreaseMax.Checked)
                                dt = ApDal.ExecuteStoredProcedureMaxIncrease(
                                        tbx_机床型号.Text.Trim(),
                                        tbx_A号.Text.Trim(),
                                        string.Empty,
                                        string.Empty,
                                        s_用户名,
                                        tbx_审核人.Text.Trim(),
                                        0,
                                        string.Empty);
                            else
                                dt = ApDal.ExecuteStoredProcedure(
                                        tbx_机床型号.Text.Trim(),
                                        tbx_A号.Text.Trim(),
                                        string.Empty,
                                        string.Empty,
                                        s_用户名,
                                        tbx_审核人.Text.Trim(),
                                        0,
                                        string.Empty);
                            if (dt != null)
                            {
                                if (dt.Rows.Count == 1)
                                {
                                    string info = string.Format("机床：{0} A号：{1}的图纸：ZP-4C，编号分配【成功】，编号为：{2}", tbx_机床型号.Text.Trim(), tbx_A号.Text.Trim(), (string)dt.Rows[0]["图纸编号"]);
                                    //cadApp.ShowAlertDialog(info);
                                    this.labelOut.Text = info;
                                }
                            }
                        }
                        else
                        {
                            if (dtCheck != null)
                            {
                                string info = string.Format("机床：{0} A号：{1}的图纸：ZP-4C，编号已经【存在】，编号为：{2}", tbx_机床型号.Text.Trim(), tbx_A号.Text.Trim(), (string)dtCheck.Rows[0]["图纸编号"]);
                                this.labelOut.Text = info;
                            }
                        }
                    }
                }
                else
                {
                }
                statusDal.UnLock(s_用户名);
            }
            //XSL ADD 不输出明细表了。
            //OutExcelForm outexcelform = new OutExcelForm();
            //outexcelform.Jch = tbx_机床型号.Text.Trim();
            //outexcelform.Ah = tbx_A号.Text.Trim();
            //outexcelform.User = s_用户名;
            //outexcelform.Check4C = true;
            //outexcelform.ShowDialog();
         }

        private void Apply4CNumberNoDwg_FormClosed(object sender, FormClosedEventArgs e)
        {
            StatusDal statusDal = new StatusDal();
            statusDal.UnLock(s_用户名);
            string filePath = System.Reflection.Assembly.GetExecutingAssembly().Location;
            string file = Path.Combine(Path.GetDirectoryName(filePath), "config.ini");
            if (File.Exists(file))
            {
                FileIni t = new FileIni(file);
                t.WriteValue("配置", "4C无图机床", tbx_机床型号.Text);
                t.WriteValue("配置", "4C无图A号", tbx_A号.Text);
            }
        }
    }
}
